/*
 *  vector.cc
 *  wonderland-project
 *
 *  Created by Ben Nolan on 8/10/08.
 *  Copyright 2008 Nolan Consulting Limited. All rights reserved.
 *
 */

#include <v8.h>
#include <OgreVector3.h>
#include <OgreQuaternion.h>
#include "vector.h"
#include "v8defines.h"
#include "scripting_context.h"

namespace JSVector{

  v8::Handle<v8::ObjectTemplate> instance_template;
  v8::Handle<v8::FunctionTemplate> function_template;
  v8::Persistent<v8::Context> context_;

  V8CALLBACK(Vector,getDistance){ 
    if (!V8ARGUMENTTYPE("Vector", 0))
      return ThrowException(v8::String::New("Invalid argument"));

    return v8::Number::New(V8INTERNAL(Ogre::Vector3*)->distance(*V8ARGUMENT(Ogre::Vector3*, 0)));
  }

  V8CALLBACK(Vector,getLength){ 
    return v8::Number::New(V8INTERNAL(Ogre::Vector3*)->length());
  }

  V8CALLBACK(Vector,getNormalized){ 
    return JSVector::New(V8INTERNAL(Ogre::Vector3*)->normalisedCopy());
  }

  V8_GETTER(Vector,x){
    return v8::Number::New(V8INTERNAL(Ogre::Vector3*)->x);
  }

  V8_SETTER(Vector,x){
    V8INTERNAL(Ogre::Vector3*)->x = value->NumberValue();
  }

  V8_GETTER(Vector,y){
    return v8::Number::New(V8INTERNAL(Ogre::Vector3*)->y);
  }

  V8_SETTER(Vector,y){
    V8INTERNAL(Ogre::Vector3*)->y = value->NumberValue();
  }

  V8_GETTER(Vector,z){
    return v8::Number::New(V8INTERNAL(Ogre::Vector3*)->z);
  }

  V8_SETTER(Vector,z){
    V8INTERNAL(Ogre::Vector3*)->z = value->NumberValue();
  }

  /* Sets internal fields with our idiom of storing the class name in the second field */
  void setInternalFields(v8::Persistent<v8::Object> p, Ogre::Vector3 v){
    p->SetInternalField(0, v8::External::New(new Ogre::Vector3(v.ptr())));
    p->SetInternalField(1, v8::String::New("Vector"));
  }
  
  /* Construct a new instance of a vector */
  V8CALLBACK(Vector,create){
    if (!args.IsConstructCall()) 
      return ThrowException(v8::String::New("Cannot call constructor as function"));

    v8::Persistent<v8::Object> self = v8::Persistent<v8::Object>::New(args.Holder());

    if (args.Length() == 3){
      setInternalFields(self, Ogre::Vector3(
        args[0]->NumberValue(), 
        args[1]->NumberValue(), 
        args[2]->NumberValue()
      ));
    }else{
      setInternalFields(self, Ogre::Vector3::ZERO);
    }

    return self;
  }

  /* Called from c++ */
  v8::Handle<v8::Value> New(Ogre::Vector3 v){
    context_->Enter();

    v8::Persistent<v8::Object> instance = v8::Persistent<v8::Object>::New(function_template->GetFunction()->NewInstance());
    setInternalFields(instance, v);
    return instance;
  }

  /* Create the function and instance templates and register the Vector constructor */

  void Initialize(Wonderland::ScriptingContext*sc){
    context_ = sc->getContext();

    // Function Template
    function_template = v8::FunctionTemplate::New(Vector_create);
    function_template->SetClassName(v8::String::New("Vector"));

    // Instance template
    instance_template = function_template->InstanceTemplate();
    instance_template->SetInternalFieldCount(2);

    instance_template->SetAccessor(v8::String::New("x"), Vector_getx, Vector_setx);
    instance_template->SetAccessor(v8::String::New("y"), Vector_gety, Vector_sety);
    instance_template->SetAccessor(v8::String::New("z"), Vector_getz, Vector_setz);
    instance_template->Set("getDistance", v8::FunctionTemplate::New(Vector_getDistance));
    instance_template->Set("getLength", v8::FunctionTemplate::New(Vector_getLength));
    instance_template->Set("getNormalized", v8::FunctionTemplate::New(Vector_getNormalized));

    // add the constructor
    context_->Enter();
    context_->Global()->Set(v8::String::New("Vector"), function_template->GetFunction());
  }

}